from time import time
from multiprocessing import Pool
from os import cpu_count
from tqdm import tqdm

default_worker_num = 2 * cpu_count() - 1


def parallel_map(func, data, workers: int = default_worker_num, chunksize=None, **kwargs):
    """
    并行处理
    :param func: 函数
    :param data: 数据
    :param workers: 进程数量
    :param chunksize:
    :param kwargs:
    :return:
    """
    print('Initializing multi-process...')
    begin = time()
    pool = Pool(workers, **kwargs)
    results = pool.map(func, data, chunksize=chunksize)
    pool.close()
    pool.join()
    gap = time() - begin
    print('Done.')
    print('Elapsed time: {} min {:.2f} sec'.format(int(gap // 60), gap % 60))
    return results


def bar_parallel_map(func, data, workers):
    pool = Pool(workers)
    mapped_values = list(tqdm(pool.imap(func, data), total=len(data)))
    return mapped_values
